package com.sso.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


public class XMLUtil {

	private static final String ENCODE= "UTF-8" ;

	public static void updateNode(String filePath ,Document doc){
		XMLWriter writer = null ;
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(ENCODE);
			writer = new XMLWriter(new FileWriter(filePath), format);
			writer.write(doc);
		} catch (Exception e) {
			e.printStackTrace() ;
			throw new RuntimeException(filePath + "SAVE FAILED ") ;
		}finally{
			try {
				if(writer!=null)
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Document getDocument(String filePath){
		SAXReader sax  = new SAXReader() ;
		Document doc  = null;
		File f  =  null ;
		try {
			 f = new File(filePath);
			 doc = sax.read(f) ;
		} catch (DocumentException e) {
			throw new RuntimeException(filePath + " NOT FOUND") ;
		}
		return doc;
	}
}


